#include <stdio.h>
#include <curl/curl.h>
#include <cjson/cJSON.h>


// 回调函数用于接收响应数据
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
    size_t realsize = size * nmemb;
    // 这里可以处理获取到的数据，比如存储到文件或者其他操作
    char * result = (char *)contents;
    cJSON* root = cJSON_Parse(result);
    cJSON* successJson = cJSON_GetObjectItemCaseSensitive(root,"success");
    cJSON* codeJson = cJSON_GetObjectItemCaseSensitive(root,"code");
    cJSON* messageJson = cJSON_GetObjectItemCaseSensitive(root,"message");
    printf("success=%s ,code=%d ,message =%s\n",successJson->valuestring,codeJson->valueint,messageJson->valuestring);
    return realsize;
}

int main(){
   CURL* curl = curl_easy_init();
   if (curl)
   {
        const char *post_url = "http://47.109.65.208/config.json";
        const char *post_data = "key1=value1&key2=value2";
        char response[512]; // 假设响应不会超过512字节

        curl_easy_setopt(curl, CURLOPT_URL, post_url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
        CURLcode res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
    }
    //清理
        curl_easy_cleanup(curl);
   }
   return 0;
   
}